package cn.jetpiece.cloud.system.dp.app.modular.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.jetpiece.cloud.codegenerate.generate.impl.CodeGenerateOne;
import cn.jetpiece.cloud.codegenerate.generate.impl.base.BaseCodeGenerate;
import cn.jetpiece.cloud.core.page.PageFactory;
import cn.jetpiece.cloud.core.query.QueryGenerator;
import cn.jetpiece.cloud.core.util.HttpContext;
import cn.jetpiece.cloud.model.page.PageResult;
import cn.jetpiece.cloud.model.response.Result;
import cn.jetpiece.cloud.online.app.modules.cgform.controller.OnlCgformApiController;
import cn.jetpiece.cloud.online.app.modules.cgform.service.impl.OnlCgformHeadServiceImpl;
import cn.jetpiece.cloud.online.app.modules.low.aspect.LowAppFilter;
import cn.jetpiece.cloud.system.dp.api.biz.DpTSystemApi;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.CreateDpTSystemParam;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.UpdateDpTSystemParam;
import cn.jetpiece.cloud.system.dp.api.biz.model.query.DpTAppQuery;
import cn.jetpiece.cloud.system.dp.api.biz.model.query.DpTSystemQuery;
import cn.jetpiece.cloud.system.dp.api.biz.model.result.DpTAppResult;
import cn.jetpiece.cloud.system.dp.api.biz.model.result.DpTSystemResult;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTApp;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTSystem;
import cn.jetpiece.cloud.system.dp.app.modular.biz.service.IDpTSystemService;
import cn.jetpiece.cloud.system.dp.app.modular.biz.wrapper.DpTAppWrapper;
import cn.jetpiece.cloud.system.dp.app.modular.biz.wrapper.DpTSystemWrapper;
import cn.jetpiece.cloud.starter.app.modular.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import freemarker.core.InvalidReferenceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.authenticator.AuthenticatorBase;
import org.apache.catalina.connector.CoyoteAdapter;
import org.apache.catalina.core.ApplicationFilterChain;
import org.apache.catalina.valves.ErrorReportValve;
import org.apache.coyote.AbstractProcessorLight;
import org.apache.coyote.http11.Http11Processor;
import org.apache.tomcat.util.net.NioEndpoint;
import org.apache.tomcat.util.net.SocketProcessorBase;
import org.apache.tomcat.util.threads.TaskThread;
import org.apache.tomcat.websocket.server.WsFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.filter.RequestContextFilter;
import org.springframework.web.method.support.InvocableHandlerMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod;

import javax.servlet.http.HttpServlet;
import javax.validation.Valid;
import java.lang.annotation.Native;
import java.util.List;

/**
 * 子系统信息表
 *
 * @author jetpiece
 * @since 2023-05-24
 */
@Slf4j
@RestController
public class DpTSystemController extends BaseController<DpTSystem, IDpTSystemService> implements DpTSystemApi {
	@Autowired
	private IDpTSystemService dpTSystemService;

    @Override
	public Result<String> add(CreateDpTSystemParam param) {
	    try {
	        DpTSystem dpTSystem = BeanUtil.toBean(param, DpTSystem.class);
	        if (dpTSystemService.save(dpTSystem)) {
                return Result.OK("添加成功!");
	        }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Result.error("添加失败");
	}

	@Override
    public Result<String> update(UpdateDpTSystemParam param) {
	    try {
	        DpTSystem oldBean = dpTSystemService.getById(param.getSystemId());
	        if (oldBean == null) {
                return Result.error("数据未找到!");
	        }
	        DpTSystem dpTSystem = BeanUtil.toBean(param, DpTSystem.class);
	        if (dpTSystemService.updateById(dpTSystem)) {
                return Result.OK("编辑成功!");
	        }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Result.error("编辑失败");
	}

	@Override
	public Result<String> delete(Long id) {
		try {
			if (dpTSystemService.removeById(id)) {
				return Result.OK("删除成功!");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return Result.error("删除失败");
	}

	@Override
	public Result<String> deleteBatch(String ids) {
		try {
			List<Long> idList = Convert.toList(Long.class, ids.split(","));
			if (dpTSystemService.removeByIds(idList)) {
				return Result.OK("批量删除成功!");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return Result.error("批量删除失败");
	}

	@Override
	public Result<DpTSystemResult> get(Long id) {
		try {
			DpTSystem dpTSystem = dpTSystemService.getById(id);
			if (dpTSystem == null) {
				return Result.error("数据未找到!");
			}
			return Result.OK(BeanUtil.toBean(dpTSystem, DpTSystemResult.class));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return Result.error("获取数据失败！");
	}

	@Override
	public Result<PageResult<DpTSystemResult>> page(DpTSystemQuery query) {
		DpTSystem dpTSystem = BeanUtil.toBean(query, DpTSystem.class);
		QueryWrapper<DpTSystem> queryWrapper = QueryGenerator.initQueryWrapper(dpTSystem, HttpContext.getRequestParameters());
		Page<DpTSystem> page = PageFactory.defaultPage();
		IPage<DpTSystem> pageList = dpTSystemService.page(page, queryWrapper);
		return Result.OK(new DpTSystemWrapper(pageList).setWrapper(true).wrapPageResult());
	}

}